package com.question.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.question.Constant.BaseConstant;
import com.question.Dto.PaperAnswerDto;
import com.question.Exception.BaseException;
import com.question.Vo.SessionWebUserDto;
import com.question.annotation.GlobalInterceptor;
import com.question.common.PageResult;
import com.question.common.Result;
import com.question.entity.Paper;
import com.question.entity.PaperUser;
import com.question.service.PaperUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.List;

@RestController
@RequestMapping("/paperuser")
public class PaperUserController {
    @Resource
    private PaperUserService paperUserService;

    @RequestMapping("/add")
   @GlobalInterceptor(checkLogin = true,checkParam = true)
    public Result add(Integer pid, Integer uid) {
        paperUserService.adds(pid, uid);
        return Result.success();
    }
    @RequestMapping("/list")
    @GlobalInterceptor(checkLogin = true,checkParam = true)
    public Result list(Integer pageNo,Integer pageSize){
        PageResult pageResult=new PageResult<>();
        pageResult=paperUserService.getpage(pageNo,pageSize);
        return Result.success(pageResult);
    }
    @RequestMapping("/delete")
    @GlobalInterceptor(checkLogin = true,checkParam = true)
    public Result delete(Integer id){

        PaperUser paperUser=paperUserService.getById(id);
        if(paperUser ==null){
            throw new BaseException("该记录不存在");
        }
        paperUserService.removeById(id);
        return Result.success();
    }
    @RequestMapping("/userlist")
    @GlobalInterceptor(checkLogin = true,checkParam = true)
    public Result userlist(HttpSession httpSession){
        SessionWebUserDto sessionWebUserDto= (SessionWebUserDto) httpSession.getAttribute(BaseConstant.key);
        Integer id=sessionWebUserDto.getId();
        List<Paper> list=paperUserService.getByUserId(id);

        return Result.success(list);

    }
    @RequestMapping("/userfinishlist")
    @GlobalInterceptor(checkLogin = true,checkParam = true)
    public Result userfinishlist(HttpSession httpSession){
        SessionWebUserDto sessionWebUserDto= (SessionWebUserDto) httpSession.getAttribute(BaseConstant.key);
        Integer id=sessionWebUserDto.getId();
        List<Paper> list=paperUserService.getfinishByUserId(id);

        return Result.success(list);

    }

    @RequestMapping("/submit")
    @GlobalInterceptor(checkLogin = true,checkParam = true)
    public Result submit(HttpSession httpSession,@RequestBody PaperAnswerDto paperAnswerDto){
        SessionWebUserDto sessionWebUserDto= (SessionWebUserDto) httpSession.getAttribute(BaseConstant.key);
        Integer id=sessionWebUserDto.getId();
        paperUserService.submit(id,paperAnswerDto);
        return Result.success();

    }



}
